package com.xiaoyu.im.datamodel;

import androidx.annotation.IntDef;

import android.util.SparseArray;

import in.srain.cube.util.StringUtil;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MessageStatus {

    public static final int INIT = 0;
    public static final int CHECKED = 1;
    public static final int PREPARING = 2;
    public static final int PREPARED = 3;
    public static final int SENDING = 4;
    public static final int CHECKING = 7;

    public static final int SUCCESS = 5;

    public static final int SDK_FAILED = 6;
    public static final int BLACKED_FAILED = 8;
    public static final int BANNED_FAILED = 9;
    public static final int LOCAL_FORBIDDEN_FAILED = 10;
    public static final int UPLOAD_RESOURCE_FAILED = 11;
    public static final int NOT_CID_FAILED = 12;
    public static final int NOT_CONVERSATION_FAILED = 13;
    public static final int COMPRESS_RESOURCE_FAILED = 15;
    public static final int PRISM_LIMIT_FAILED = 16;
    public static final int NETWORK_INVALID = 17;

    @IntDef({INIT, CHECKED, PREPARING, PREPARED, SENDING, CHECKING, SUCCESS, SDK_FAILED, BLACKED_FAILED, BANNED_FAILED, LOCAL_FORBIDDEN_FAILED, UPLOAD_RESOURCE_FAILED, NOT_CID_FAILED, NOT_CONVERSATION_FAILED, COMPRESS_RESOURCE_FAILED, PRISM_LIMIT_FAILED, NETWORK_INVALID})
    public @interface MessageStatusDef {
    }

    private static final Set<Integer> FAIL_STATUS = new HashSet<>(Arrays.asList(SDK_FAILED, BLACKED_FAILED, BANNED_FAILED, LOCAL_FORBIDDEN_FAILED, UPLOAD_RESOURCE_FAILED, NOT_CID_FAILED, NOT_CONVERSATION_FAILED, COMPRESS_RESOURCE_FAILED, PRISM_LIMIT_FAILED, NETWORK_INVALID));

    public static boolean isFailedStatus(int status) {
        return FAIL_STATUS.contains(status);
    }

    private static final SparseArray<String> TO_STRING = new SparseArray<>();

    static {
        TO_STRING.put(INIT, "init");
        TO_STRING.put(CHECKED, "checked");
        TO_STRING.put(PREPARING, "preparing");
        TO_STRING.put(PREPARED, "prepared");
        TO_STRING.put(SENDING, "sending");
        TO_STRING.put(CHECKING, "checking");
        TO_STRING.put(SUCCESS, "success");
        TO_STRING.put(SDK_FAILED, "sdk_failed");
        TO_STRING.put(BLACKED_FAILED, "blacked_failed");
        TO_STRING.put(BANNED_FAILED, "banned_failed");
        TO_STRING.put(LOCAL_FORBIDDEN_FAILED, "local_forbidden_failed");
        TO_STRING.put(UPLOAD_RESOURCE_FAILED, "upload_resource_failed");
        TO_STRING.put(NOT_CID_FAILED, "not_cid_failed");
        TO_STRING.put(NOT_CONVERSATION_FAILED, "not_conversation_failed");
        TO_STRING.put(COMPRESS_RESOURCE_FAILED, "compress_resource_failed");
        TO_STRING.put(PRISM_LIMIT_FAILED, "prism_limit_failed");
        TO_STRING.put(NETWORK_INVALID, "network_invalid_failed");
    }

    public static String toString(int status) {
        return StringUtil.blankIfNull(TO_STRING.get(status));
    }

    public static String getTypeForLog(int status) {
        switch (status) {
            case SDK_FAILED:
            case BLACKED_FAILED:
            case BANNED_FAILED:
            case UPLOAD_RESOURCE_FAILED:
                return TO_STRING.get(status);
            default:
                return "unknown";
        }
    }
}
